package com.example.administrator.fangbook.importlocal.fragment;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.administrator.fangbook.HtlMvp.fragment.BaseMvpFragment;
import com.example.administrator.fangbook.R;
import com.example.administrator.fangbook.appliction.HtlApplication;
import com.example.administrator.fangbook.dataLibrary.book.IBookbase;
import com.example.administrator.fangbook.dataLibrary.book.ShelfItemBook;
import com.example.administrator.fangbook.importlocal.bean.FileNode;
import com.example.administrator.fangbook.importlocal.fragment.adapter.CapacityAdapter;
import com.example.administrator.fangbook.importlocal.fragment.presenter.ImportLocalPresenter;
import com.example.administrator.fangbook.importlocal.fragment.view.ImportLocalView;
import com.example.administrator.fangbook.importlocal.utils.FileType;
import com.example.administrator.fangbook.importlocal.utils.ScanSdUtil;
import com.example.administrator.fangbook.utils.ToastUtils;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class ImportLocalCapacityFragment extends BaseMvpFragment<ImportLocalView, ImportLocalPresenter> implements ImportLocalView {

    @Bind(R.id.capacity_recyclerView)
    RecyclerView capacityRecyclerView;
    @Bind(R.id.ll_loading)
    LinearLayout lLoadding;
    private CapacityAdapter mCapacityAdapter;

    public static ImportLocalCapacityFragment getFragment(Bundle bundle) {
        ImportLocalCapacityFragment fragment = new ImportLocalCapacityFragment();
        if (bundle != null) {
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    protected ImportLocalPresenter bindMvpPresenter() {
        return new ImportLocalPresenter();
    }

    @Override
    protected ImportLocalView bindMvpView() {
        return this;
    }


    @Override
    protected int getLayoutID() {
        return R.layout.import_local_capacity_layout;
    }

    @Override
    protected void initFragment(Bundle arguments) {
        initRecyclerView();
        //设置数据，扫描本地
        File root = Environment.getExternalStorageDirectory();
        if (root != null) {
            //开启排序add子线程
            getmMvpPresenter().startSortToAddThread();
            //开始扫描--开启子线程
            getmMvpPresenter().startScanThread();
            //开始扫描
            getmMvpPresenter().startScan(new FileNode(null, root));

        }

    }

    private void initRecyclerView() {
        capacityRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mCapacityAdapter = new CapacityAdapter(getActivity());
        mCapacityAdapter.setOnItemClickLisener(new CapacityAdapter.OnItemClickLisener() {
            @Override
            public void onItemClick(FileNode finalCurrFileNode) {
                addLibrary(finalCurrFileNode);
            }
        });
        capacityRecyclerView.setAdapter(mCapacityAdapter);
    }

    private void addLibrary(FileNode finalCurrFileNode) {
        ShelfItemBook book = new ShelfItemBook();
        book.setBookId(finalCurrFileNode.getPath());
        book.setBookType(getBookType(finalCurrFileNode.getType()));
        book.setName(finalCurrFileNode.getName());
        book.setAuthorName(finalCurrFileNode.getAuthor());
        try {
            HtlApplication.getApplication().getmDataHelper().getShelfBookDao().createIfNotExists(book);
            ToastUtils.show("添加成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private IBookbase.BookType getBookType(FileType fileType) {
        IBookbase.BookType type;
        if (FileType.EPUB.equals(fileType)) {
            type = IBookbase.BookType.Type_Epub;
        } else if (FileType.CHINESE_ALL.equals(fileType)) {
            type = IBookbase.BookType.Type_ChineseAll;
        } else {
            type = IBookbase.BookType.Type_Txt;
        }
        return type;
    }

    @Override
    public void setAdapter(ArrayList<FileNode> mFileNodes) {
        lLoadding.setVisibility(View.GONE);
        mCapacityAdapter.setList(mFileNodes);
    }
}
